<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-head-widget" :style="getStyle('head')">
      <div class="logo-wrap item" :style="getStyle('picture')">
        <img class="logo" :src="pictureResource" alt="企业Logo"
             v-if="(!!(optionsInfo?.imageUrl?.val) || (optionsInfo?.imageUrl))"/>
      </div>
      <template v-if="(optionsInfo.contentShow)">
        <div class="title item"
             :style="getStyle('title')"
             v-if="!!(optionsInfo.content || headTitle)"
             :data-content="(optionsInfo.content || headTitle)"
        >
          <NoticeBar :text="(optionsInfo.content || headTitle)" :style="deptStyle"/>
        </div>
      </template>
      <div class="now item" :class="timeClassName(optionsInfo.changePosition)">
        <div class="time">
          <div class="time-info">{{ time }}</div>
          <div class="data-info">{{ date }}</div>
        </div>
        <div class="sec" v-if="(optionsInfo.dateShow)">
          <div class="week">{{ getWeekInfo(week) }}</div>
          <div class="date">{{ date }}</div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import dayjs from 'dayjs';
import {NoticeBar} from 'vant';
import emitter from "@/utils/designer/emitter";
import {getWeek, pxToVw} from '@/utils/common';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";

let buildParent = buildParentApi();

export default {
  name: "scene-head-blue-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    NoticeBar
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: () => {
        return false
      },
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },

    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },

    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },

    visibleTitle: {
      type: Boolean,
    },

    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      week: '',
      optionsInfo: Object(),
      mockOpen: false,
      date: dayjs().format('YYYY年MM月DD日'),
      time: dayjs().format('HH:mm'),
    }
  },

  computed: {
    timeClassName() {
      return function (val) {
        return val ? 'date-time' : 'time-date'
      }
    },

    deptStyle() {
      return {
        fontWeight: this.optionsInfo.deptFontWeight,
        color: this.optionsInfo.deptFontColor,
        fontSize: pxToVw(this.optionsInfo.deptFontSize)
      }
    },

    timeType() {
      return this.optionsInfo.timeValue
    },

    // 获取日期
    getWeekInfo() {
      return function (info) {
        return getWeek(info)
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
          case 'title':
            Reflect.set(Obj, 'fontSize', pxToVw(info.fontSize));
            // Reflect.set(Obj, 'fontWeight', pxToVw(info.deptFontWeight));
            return Obj;
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            Reflect.set(Obj, 'color', `${(info.fontColor)}`);
            return Obj;
        }
      }
    },

    //获取LOGO资源
    pictureResource() {
      let {imageUrl} = this.optionsInfo;
      if (this.$lodash.isObject(imageUrl)) {
        return imageUrl?.val?.url || imageUrl?.url;
      } else {
        return imageUrl;
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.updateTime();
    this.handleOnMounted();
    // this.executeMethod('headerInfo','')
  },

  methods: {
    // 获取时间
    updateTime(val) {
      let timeType = (this.optionsInfo.timeValue);
      if (!!val) {
        let msg = JSON.parse(val);
        if (!!msg && !!(msg.timestamp)) {
          let timestamp = dayjs.unix(msg.timestamp);
          [this.week, this.date, this.time] = dayjs(timestamp).format(`d,${timeType},HH:mm`).split(',');
        } else {
          [this.week, this.date, this.time] = dayjs().format(`d,${timeType},HH:mm`).split(',');
        }
      } else {
        [this.week, this.date, this.time] = dayjs().format(`d,${timeType},HH:mm`).split(',');
      }
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "headerInfo":
          this.$nextTick(() => {
            buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-时间信息', key: key, data: data}));
            this.updateTime(data);
          })
          break
        case "updateInfo":

          break
      }
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = val.options.config
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    },

    // 监听时间更新
    timeType: {
      handler: function (val) {
        if (val) {
          this.updateTime()
        }
      },
      deep: true,
      immediate: true
    },
  }

};
</script>

<style lang="scss" scoped>
.scene-head-widget {
  padding: 0 30px;
  box-sizing: border-box;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  color: #333;
  height: 100%;
  overflow: hidden;

  .item {
    flex: 1;
  }

  .logo-wrap {
    display: block;
    overflow: hidden;
    align-self: center;

    img {
      height: 90px !important;
      max-width: 100% !important;
      object-fit: contain;
    }
  }

  .now {
    overflow: hidden;
    line-height: 1;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;

    .time {
      float: right;
      margin-right: 8px;
      font-size: 90px;
      box-sizing: border-box;
      vertical-align: top;
      white-space: nowrap;
      min-height: inherit;
      align-self: center;
      padding-right: 20px;
      background: rgba(0, 0, 0, 1);
      text-align: center;
      border-radius: 20px;
      overflow: hidden;
      height: auto;
      min-width: 346px;

      .time-info {
        width: 100%;
        font-size: 82px;
        padding-top: 10px;
      }

      .data-info {
        width: 100%;
        font-size: 42px;
        padding-bottom: 10px;
        padding-top: 6px;
      }
    }

    .sec {
      float: right;
      align-self: center;
      font-size: 32px;

      .week {
        text-align: right;
        white-space: nowrap;
      }

      .date {
        font-weight: 500;
        white-space: nowrap;
        box-sizing: border-box;
        margin-top: 10px;
      }
    }
  }

  //时间日期交换位置
  .date-time {
    .time {
      order: 1;
      padding-right: 0;
    }

    .sec {
      margin-right: 8px;
      padding-right: 20px;
    }
  }

  .title {
    flex: 2;
    text-align: center;
    font-size: 80px;
    height: 100%;
    line-height: 1;
    box-sizing: border-box;
    align-self: center;
    //text-shadow: -1px 1px #BBB,
    //-2px 2px #BBB,
    //-3px 3px #BBB,
    //-4px 4px #BBB,
    //-5px 5px #BBB,
    //-7px 7px 3px #0008;
    align-items: center;

    :deep(.van-notice-bar) {
      background: transparent;
      color: black;
      height: 100%;
      width: 100%;
      line-height: 90px;

      .van-notice-bar__wrap {
        height: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: center;

        > div {
          @include fontWeight();
        }

      }
    }
  }

}
</style>
